package com.mofang.powerdekorhelper.activity.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.persenter.ChangePhonePerenster;
import com.mofang.powerdekorhelper.utils.Countdown;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.StringUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ChangePhoneView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2016/7/29 0029.
 */


public class ChangePhoneSecondAcitvity extends MvpActivity<ChangePhoneView, ChangePhonePerenster> implements ChangePhoneView {

    @BindView(R.id.change_phone)
    TitleBar mTitleBar;

    @BindView(R.id.change_phone_second_account_edit)
    EditText accountEdit;

    @BindView(R.id.change_phone_second_second_get_code)
    EditText codeEdit;

    @BindView(R.id.change_second_phone_get_code)
    TextView validateTv;

    String phone;

    private Countdown timer;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_change_phone_second_layout);
        initTitleBarWithback(mTitleBar, R.string.change_phone);
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.change_second_phone_get_code, R.id.change_phone_second_sure})
    void OnClick(View view) {

        phone = accountEdit.getText().toString();
        String code = codeEdit.getText().toString();

        switch (view.getId()) {

            case R.id.change_second_phone_get_code:

                if (!TextUtils.isEmpty(phone) && phone.length() == 11 && StringUtils.isPhoneNumAvaliable(phone)) {
                    // 先验证手机号是否已被使用
                    //presenter.getIsAccountExists(phone);
                    presenter.getMesCode(phone);
                } else {
                    T.showShort(this, "请输入正确的手机号");
                }

                break;
            case R.id.change_phone_second_sure:

                int id = SharePerforenceUtils.getInstance(this).getUserID();

                if (!TextUtils.isEmpty(phone) & !TextUtils.isEmpty(code)) {
                    presenter.getChangePhoneResult(id, code, phone, 1);
                }

                break;
        }
    }

    @Override
    public void isAccountExists(String accountInfo) {
        if (accountInfo != null) {

            try {
                JSONObject jsonObject = new JSONObject(accountInfo);
                String code = jsonObject.getString("code");
                String message = jsonObject.getString("message");
                if (code.equals("0")) {
                    // 账号未被注册，进行修改
                    presenter.getMesCode(phone);
                    //verifyBtnState(false, R.color.grey_deep, "");
                } else {
                    // 账号已被注册
                    T.showShort(this, "抱歉,手机号已被使用");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setVcodeResult(String msg) {
        if (msg != null & !msg.equals("")) {
            if (msg.contains("成功")) {
                T.showShort(this, "验证码已发送,请稍候");
                timer = new Countdown();
                timer.createTimer(new Countdown.UpDataTime() {
                    @Override
                    public void updataTime(int time) {
                        L.i("SetMesCode", "timer----------------->" + time);
                        validateTv.setText(time + "秒后重发");
                        if (time > 0) {
                            validateTv.setEnabled(false);
                        } else {
                            validateTv.setText("重新发送");
                            validateTv.setEnabled(true);
                        }
                    }
                });
            }
        } else {
            T.showShort(this, "发送失败，请重试");
            //verifyBtnState(true, R.color.grey_deep, "");
        }
    }

    @Override
    public void setChangeBindPhone(ResultMessage info) {
        T.showShort(this, "修改绑定手机成功");
        SharePerforenceUtils.getInstance(this).setPhone(phone);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.stop();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (timer != null) {
            timer.stop();
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {

    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    public ChangePhonePerenster initPresenter() {
        return new ChangePhonePerenster();
    }
}
